define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiluhouse/market/house/index' + location.search,
                    add_url: 'xiluhouse/market/house/add',
                    edit_url: 'xiluhouse/market/house/edit',
                    del_url: 'xiluhouse/market/house/del',
                    multi_url: 'xiluhouse/market/house/multi',
                    dragsort_url: '',
                    table: 'xiluhouse_market_house',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'village_id', title: __('基本信息'),align:'left',operate:false,formatter: function (value, row, index) {
                                var html='';
                                html+='<div>小区:'+ row.village.name+'</div>';
                                html+='<div>'+__('House_no')+':'+row.house_no+'</div>';
                                html+='<div>'+__('Space')+':'+row.space+'</div>';
                                html+='<div>'+__('Total_price')+':'+row.total_price+'万元</div>';
                                html+='<div>'+__('Price')+':'+row.price+'元</div>';
                                return html;
                            }},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'user.nickname', title: __('User_id'),operate: "LIKE"},
                        {field: 'house_no', title: __('House_no'), visible: false},
                        {field: 'space', title: __('Space'), operate:'BETWEEN', visible: false},
                        {field: 'total_price', title: __('Total_price'), operate:'BETWEEN', visible: false},
                        {field: 'price', title: __('Price'), operate:'BETWEEN', visible: false},
                        {field: 'room_num', title: __('Room_num'), operate:false},
                        {field: 'ting_num', title: __('Ting_num'), operate:false},
                        {field: 'wei_num', title: __('Wei_num'), operate:false},
                        {field: 'face', title: __('Face'), operate: 'LIKE'},
                        {field: 'period', title: __('Period'), operate: 'LIKE'},
                        {field: 'is_elevator', title: __('Is_elevator'), searchList: {"0":__('Is_elevator 0'),"1":__('Is_elevator 1')}, formatter: Table.api.formatter.normal},
                        {field: 'total_floor', title: __('Total_floor'), operate:false},
                        {field: 'floor_num', title: __('Floor_num'), operate:false},
                        //{field: 'purpose_id', title: __('Purpose_id')},
                        {field: 'is_unique', title: __('Is_unique'), searchList: {"0":__('Is_unique 0'),"1":__('Is_unique 1')}, formatter: Table.api.formatter.normal},
                        {field: 'buy_years', title: __('Buy_years'), operate:false},
                       // {field: 'decoration_id', title: __('Decoration_id')},
                        {field: 'parking_lot', title: __('Parking_lot'), operate:false},
                        {field: 'property_right', title: __('Property_right'), searchList: {"1":__('Property_right 1'),"2":__('Property_right 2')}, formatter: Table.api.formatter.normal},
                        {field: 'view_status', title: __('View_status'), searchList: {"1":__('View_status 1'),"2":__('View_status 2'),"3":__('View_status 3'),"4":__('View_status 4')}, formatter: Table.api.formatter.normal},
                        {field: 'video_file', title: __('Video_file'), operate: false, formatter: Table.api.formatter.files},
                        {field: 'state', title: __('State'), searchList: {"1":__('State 1'),"2":__('State 2'),"3":__('State 3')}, formatter: Table.api.formatter.normal},
                        {field: 'fav_count', title: __('Fav_count'), operate:false},
                        {field: 'view_num', title: __('View_num'), operate:false},
                        {field: 'collection_num', title: __('Collection_num'), operate:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Status normal'),"hidden":__('Status hidden')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'xiluhouse/market/house/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'xiluhouse/market/house/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'xiluhouse/market/house/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
